package com.koo.common.utils;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.koo.common.exception.RRException;
import com.koo.modules.comm.entity.CommUploadRecordEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SynDataUtil {

    public static JSONObject downloadProductData(Long storeId, List<CommUploadRecordEntity> records) {
        Map<String, Object> map = configArgs(new String[]{"storeId", "downloadRecords"},
                new Object[]{storeId, records});
        configSystemParam(map, "api.third.data.get.downloadProductData");
        return unWrapData(map);
    }

    public static JSONObject downloadOutStorageData(String storeId, List<CommUploadRecordEntity> records) {
        Map<String, Object> map = configArgs(new String[]{"storeId", "downloadRecords"},
                new Object[]{storeId, records});
        configSystemParam(map, "api.third.data.get.downloadOutOrderData");
        return unWrapData(map);
    }

    public static Integer getDownloadCount(String tableName, String storeId, List<CommUploadRecordEntity> records) {
        Map<String, Object> map = configArgs(new String[]{"tableName", "storeId", "downloadRecords"},
                new Object[]{tableName, storeId, records});
        configSystemParam(map, "api.third.data.get.findDownloadCount");
        try {
            String result = HttpClientUtil.postJson(PropFileConfig.getProperty("db.syn.url") + "/api/third/gateway.action", JSON.toJSONString(map));
            JSONObject jsonObject = JSON.parseObject(result);
            if (jsonObject.getInteger("code") == 200 && StringUtil.isEmpty(jsonObject.getJSONObject("responseBody").getString("msg"))) {
                return jsonObject.getJSONObject("responseBody").getInteger("count");
            }
        } catch (Exception e) {
        }
        return 0;
    }

    private static JSONObject unWrapData(Map<String, Object> map) {
        String result = HttpClientUtil.postJson(PropFileConfig.getProperty("db.syn.url") + "/api/third/gateway.action", JSON.toJSONString((map)));
        JSONObject jsonObject = JSON.parseObject(result);
        if (jsonObject.getInteger("code") == 200) {
            JSONObject responseBody = jsonObject.getJSONObject("responseBody");
            if (responseBody.getInteger("state") == 200) {
                return responseBody;
            }
            throw new RRException(responseBody.getString("msg"));
        }
        throw new RRException(jsonObject.getString("msg"));
    }

    private static Map<String, Object> configArgs(String[] fields, Object[] datas) {
        Map<String, Object> map = new HashMap<>();
        if (fields.length == datas.length) {
            for (int i = 0, end = fields.length; i < end; i++) {
                Object dataValue = datas[i];
                if (dataValue instanceof String) {
                    map.put(fields[i], dataValue);
                } else {
                    map.put(fields[i], JSON.toJSONString(dataValue));
                }
            }
        }
        return map;
    }

    private static void configSystemParam(Map<String, Object> map, String method) {
        map.put("apikey", PropFileConfig.getProperty("db.syn.appkey"));
        map.put("timestamp", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        map.put("version", "1");
        map.put("method", method);
        map.put("sign", SignUtil.generateSignature(map, PropFileConfig.getProperty("db.syn.apisecret")));
    }

}
